package com.streetsmart.test;
/**
 *  test activity to test the various sql queries 
 */
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.streetsmart.DBManager;
import com.streetsmart.R;
import com.streetsmart.R.string;

public class testDBActivity extends ListActivity {
//public class testDBActivity extends Activity{	
	  DBManager dbhelper;
	  Cursor cur;
	 
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testdblist);
		dbhelper = new DBManager(this);
		//TextView label = (TextView) findViewById(R.id.listItem);
		//label.setText("Hello there!");
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.dboptions, menu);
        return true;
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//TextView label = (TextView) findViewById(R.id.listItem);
		//label.setText("Hello there!");
		
	    switch (item.getItemId()) {
	        case R.id.mnugetAll:
	        	cur = dbhelper.getAllEvents();
	        	//TODO startManagingCursor is deprecated.. Change to the new one later
	        	break;
	         
	        case R.id.mnugetMUST:
	        	cur = dbhelper.getAllEvents("Must");
	        	break;
	         
	        case R.id.mnugetGOOD:
	        	cur = dbhelper.getAllEvents("Good");
	        	break;
	         
	        case R.id.mnugetMUSTinBetween:
	        	cur = dbhelper.getAllMustToDoEventsBetween(100000L, 400000L);
	        	break;
	        	
	        case R.id.mnugetGOODinBetween:
	        	cur = dbhelper.getAllGoodToDoEventsBetween(100000L, 300000L);
	        	break;
	        	
	        case R.id.mnugetUnscheduled:
	        	cur = dbhelper.getAllGoodToDoEventsNotInBetween(200000L, 500000L);
	        	break;
	        	
	        case R.id.mnugetGOODUnscheduled:
	        	cur = dbhelper.getAllUnscheduledGoodToDoEvents();
	        	break;
	        	
	        case R.id.mnugetAllRecentLocations:
	        	cur = dbhelper.getAllRecentLocations();
	        	int count = cur.getCount();
	        	startManagingCursor(cur);
		    	String[] from = new String[] {"latitude"};
		    	int[] to = new int[] {R.id.listItem};
		    	SimpleCursorAdapter scadapter = new SimpleCursorAdapter(this, R.layout.testdbrow, cur, from, to);
		    	setListAdapter(scadapter);
		    	ListView listview = getListView();
		    	listview.setOnItemClickListener(new OnItemClickListener() {
	    		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                AlertDialog.Builder builder = new AlertDialog.Builder(testDBActivity.this);
	                builder.setTitle("Selection").setMessage(((TextView) view).getText())
	                        .setCancelable(false).setPositiveButton("Ok", null);
	                builder.create().show();
	            }

		    	});
		    	return true;
	        case R.id.mnugetreturnlocations:
	        	cur = dbhelper.getAllReturnLocations();
	        	count = cur.getCount();
	        	startManagingCursor(cur);
	        	from = new String[] { "latitude", "longitude", "name", "address"};
	        	to = new int[] {R.id.listItem1, R.id.listItem2, R.id.listItem3, R.id.listItem4};
	        	scadapter = new SimpleCursorAdapter(this, R.layout.simpletestdbrow, cur, from, to);
	        	setListAdapter(scadapter);
	        	listview = getListView();
		    	listview.setOnItemClickListener(new OnItemClickListener() {
	    		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                AlertDialog.Builder builder = new AlertDialog.Builder(testDBActivity.this);
	                builder.setTitle("Selection").setMessage(((TextView) view).getText())
	                        .setCancelable(false).setPositiveButton("Ok", null);
	                builder.create().show();
	            }

		    	});
		    	return true;
	        	
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    	startManagingCursor(cur);
	    	String[] from = new String[] {"name"};
	    	int[] to = new int[] {R.id.listItem};
	    	SimpleCursorAdapter scadapter = new SimpleCursorAdapter(this, R.layout.testdbrow, cur, from, to);
	    	setListAdapter(scadapter);
	    	ListView listview = getListView();
	    	listview.setOnItemClickListener(new OnItemClickListener() {
    		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(testDBActivity.this);
                builder.setTitle("Selection").setMessage(((TextView) view).getText())
                        .setCancelable(false).setPositiveButton("Ok", null);
                builder.create().show();
            }

	    	});
	    	return true;

	    }
	
	@Override
	public void onDestroy(){
		dbhelper.close();
		super.onDestroy();
	}
}
